<?php
require_once BASE_PATH . 'application/models/basedao' . EXT;

class itemDAO extends baseDAO {

    var $table = 'k2_items';
    var $key = 'id';
    var $sql = '';

    function __construct() {

        // Call the Model constructor
        parent::__construct();
    }

    function getByHits($limit = 2, $data = array()) {

        $data = array_merge($data, array(
            "limit" => array(0, $limit),
            "order" => array("hits", "ASC")
                ));

        return $this->_select($data);
    }

    function _defaultSQL($default = 1) {
	
	if($default)
	$this->db->where(array('fulltext !='=>''));
	
        $this->db->where('( publish_up="0000-00-00 00:00:00" OR publish_up <= NOW() )',NULL,false);
	$this->db->where('( publish_down="0000-00-00 00:00:00" OR publish_down >= NOW() )',NULL,false);
        $this->db->where(array("published" => 1, "trash" => 0));
	
    }
    
    
    function getCategories()
    {
        $this->_defaultSQL();
	
        $this->db->select('catid,category');
        $this->db->group_by("catid");
	$this->db->where('category !=','');
	
	$query = $this->db->get($this->table);
	return $query->result();
    }

    function findByWords($words = '', $data = array()) {
        $this->_defaultSQL();
        $data = array_merge($data, array("match" => array("title", $words)));

        return $this->_select($data);
    }

    function findByTags($tags, $data = array()) {
        $this->_defaultSQL();
        $data = array_merge($data, array("match" => array("full_tags", $tags)));

        return $this->_select($data);
    }

    function findTagsById($id, $data = array()) {
        $this->_defaultSQL();
        $record = $this->_findById($id);
	$full_tags = "";
	
	if(isset($record->full_tags))
		$full_tags = $record->full_tags;
	
        $data = array_merge($data, array("match" => array("full_tags", $full_tags)));

        return $this->_select($data);
    }

    function getColumnistByModuleId($id) {
        return $this->getResultByModuleId($id, array('where' => array('created_group' => '2')));
    }

    function setRating($id, $rating) {
        
                $this->db->set('itemID', $id);
                $this->db->set('rating_count', 'rating_count+1', FALSE);
        $this->db->set('rating_sum', $rating);
                $this->db->set('lastip', $this->input->ip_address());
        $this->db->insert('k2_rating');
                $this->db->select_avg('rating_sum');
                $this->db->where('itemID', $id);
                $this->db->from('k2_rating');
                $query = $this->db->get();
                //print_r($query->row()); die;
                $promedio = $query->row()->rating_sum;
                //////////////////////////////////////////////
                
                
        $this->db->set('total_rating', 'total_rating+1', FALSE);
        $this->db->set('rating', $promedio, FALSE);

        $this->db->where($this->key, $id);
        $this->db->update($this->table);
    }

    function setHit($id) {

        $this->db->set('hits', 'hits+1', FALSE);
        $this->db->set('last_hit', 'NOW()', FALSE);

        $this->db->where($this->key, $id);
        $this->db->update($this->table);
    }

    function delete($id) {

        if (!empty($id))
            return $this->db->update($this->table, array("published" => 0, "trash" => 1), array($this->key => $id));
        else
            return 0;
    }

    function select($data = array()) {
        $this->_defaultSQL();
        return $this->_select($data);
    }

    function findByCategory($ids = 1, $data = array()) {

        $this->_defaultSQL();
        $this->db->where_in('catid', explode(',', $ids));
        $this->_sql($data);
        $query = $this->db->get($this->table);
        return $query->result();
    }

    function getAuthorById($id) {

        $this->_defaultSQL();
        $item = $this->_findById($id);

        $this->db->limit(1);
        $this->db->like('userName', $item->created_name);
        $query = $this->db->get('k2_users');
        return $query->row();
    }

    function getIdsRelatedById($id = 1, $data = array()) {

        $this->_sql($data);
        $record = $this->_findById($id);

        if (count($record) == 0)
            return;

        return $record->audios_related;
    }

    function getNotasById($id = 1, $data = array()) {
        $ids_notas = $this->getIdsRelatedById($id);
        return $this->_findById(explode(",", $ids_notas), array("select" => "title,introtext,id"));
    }

    function getIdsRelatedIndicadoresById($id = 1, $data = array()) {

        $this->_sql($data);
        $record = $this->_findById($id);

        if (count($record) == 0)
            return;

        return $record->id_indicador;
    }

    function getIndicadoresById($id = 1, $data = array()) {
        $indicador = $this->getIdsRelatedIndicadoresById($id);
        return $this->_findById($indicador, array("select" => "id,title,introtext"));
    }

    function getAllCategoriesById($id) {

        $this->db->where('id', $id);
        $this->db->or_where('parent', $id);
        $query = $this->db->get('k2_categories');
        return $result = $query->result();
    }

    function findItemsByCategoriesId($ids = 1, $data = array()) {

        $categories = $this->getAllCategoriesById($ids);
        if ($ids == $categories[0]->id) {
            foreach ($categories as $item) {
                $this->db->or_where('catid', $item->id);
            }
        } else {
            $this->db->where_in('catid', explode(',', $ids));
        }
	$this->_defaultSQL();
        $this->_sql($data);
        $query = $this->db->get('k2_items');
        return $result = $query->result();
    }

}

?>